package com.sskj.gocoin.v.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.sskj.gocoin.R;
import com.sskj.gocoin.R2;
import com.sskj.gocoin.common.ClassicsHeader;
import com.sskj.gocoin.common.adapter.BaseAdapter;
import com.sskj.gocoin.common.adapter.ViewHolder;
import com.sskj.gocoin.common.decoration.DividerLineItemDecoration;
import com.sskj.gocoin.common.widget.ToolBarLayout;
import com.sskj.gocoin.m.bean.TransferHistory;
import com.sskj.gocoin.p.activity.TransferHistoryPresenter;
import com.sskj.gocoin.v.base.BaseActivity;

import java.text.SimpleDateFormat;
import java.util.List;

import butterknife.BindView;

/**
 * 转账记录
 *
 * @author Hey
 * create by Hey at 2018/8/7 11:21
 */
public class TransferHistoryActivity extends BaseActivity<TransferHistoryPresenter> {

    @BindView(R2.id.transfer_history_list)
    RecyclerView transferHistoryList;
    @BindView(R2.id.transfer_history_toolbar)
    ToolBarLayout toolBarLayout;
    @BindView(R2.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    private BaseAdapter<TransferHistory> adapter;
    private SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private int page = 1;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_transfer_histroy;
    }

    @Override
    public TransferHistoryPresenter getPresenter() {
        return new TransferHistoryPresenter();
    }

    @Override
    protected void initView() {
        toolBarLayout.setLeftButtonOnClickLinster(view -> {
            finish();
        });
        transferHistoryList.addItemDecoration(new DividerLineItemDecoration(this));
        transferHistoryList.setLayoutManager(new LinearLayoutManager(this));
        adapter = new BaseAdapter<TransferHistory>(R.layout.item_recycler_transfer_history, null, transferHistoryList, true) {
            @Override
            public void bind(ViewHolder holder, TransferHistory item) {
                holder.setText(R.id.transfer_id, item.getMemo())
                        .setText(R.id.transfer_time, timeFormat.format(Long.parseLong(item.getAddtime()) * 1000))
                        .setText(R.id.transfer_num, item.getPrice());
            }
        };
        ClassicsHeader classicsHeader = new ClassicsHeader(this);
        classicsHeader.setBackgroundColor(getResources().getColor(R.color.background));
        refreshLayout.setRefreshHeader(classicsHeader);
        refreshLayout.setEnableLoadMore(true);
        refreshLayout.setRefreshFooter(new ClassicsFooter(this));
        refreshLayout.setOnRefreshListener(v -> {
            page = 1;
            mPresenter.getTransferHistory(page);
        });

        refreshLayout.setOnLoadMoreListener(v -> {
            page++;
            mPresenter.getTransferHistory(page);
        });

    }

    public void stopRefresh() {
        refreshLayout.finishRefresh();
        refreshLayout.finishLoadMore();
    }

    @Override
    protected void initData() {
        mPresenter.getTransferHistory(page);
    }

    public void setHistoryList(List<TransferHistory> data) {
        if (page != 1) {
            adapter.addData(data);
        } else {
            adapter.setNewData(data);
        }
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, TransferHistoryActivity.class);
        context.startActivity(intent);
    }
}
